package bot;

import main.*;
import org.jibble.pircbot.*;
import java.lang.management.*;
import java.sql.*;
import java.util.*;

public class bot extends PircBot {
	
	//Classes
	general general = new general();
	
	//Declare generic error strings that will be used more than once
	String[] error ={	"4Error: Only bot administrators can use this command!", 
						"4Error: Only channel operators or above can use this command!", 
						"4Error: There is a fault within the API. Please try again soon.",
						"4Error: Citizen does not exist.",
						"4Error: Only channel owners can use this command!",
						"4MySQL Error: Please join #eRepBot for help.",
						"4Error: You must use !register to link your IRC/eRepublik names, or enter an eRepublik name."
					};
					
	//For using the erpk API system
	String apikey = "JmJmNYHrZ5";
	
	public Map<String, String> getTags(String message) {
		String temp[] = message.split(" ");
		Map<String, String> tags = new HashMap<String,String>();
		
		String name = "";
		for (int i = 0; i < temp.length; i++) {
			// If its a tag announcer, put it into tag list, i+1 is tag value
			if (temp[i].startsWith("-") == true) {
				tags.put(temp[i], temp[i+1]);
			// If it doesn't begin with ! or - then first check its not a tag value and append to name
			} else if (temp[i].startsWith("!") == false) {
				boolean blnExists = tags.containsValue(temp[i]);
				if (blnExists == false) {
					name = name + temp[i] + " ";
				}
			}
		}
		// Put name into tag as well
		if (name != null & name != "") {
			tags.put("name", name.trim());
		}
		return tags;
	}
	
	
	//Returns true if set manually here as bot admin	
	public boolean isBotAdmin(String hostname) {
		//CD and Mantle - add to DB soon(?)
		String[] admins = {"For.The.Greater.Good","eUSA.vUK.UKRP"};
		boolean check = false;
		for (String admin : admins) {
			if (hostname.equals(admin)) {
				check = true;
			}
		}
		return check;
	}
	
	public List<String> getChans() {
		Connection con = general.getmySQLConnection();
		List<String> chans = new ArrayList<String>();
		try {
			//Create a new statement to use
			Statement state = con.createStatement();
			//Create a ResultsSet object to take results from
			ResultSet results = state.executeQuery("SELECT * FROM channels");
			//while there is another line
			while (results.next() == true) {
				String chan = results.getString(1) + " " + results.getString(2);
				chans.add(chan);
			}
			con.close();
		} catch (Exception e) {
			//Print the Exception
				System.out.println("Exception: " + e.getMessage());
		}

		return chans;
	}
	
	public void shutDown(String hostname, String channel) {
		if (isBotAdmin(hostname) == true) {
			//leave the server
			sendRawLine("QUIT :ERRAH! L2JAVUH!");
		} else {
			//Error if not admin
			sendMessage(channel, error[0]);
		}
	}
	
	public String uptime(String hostname) {
		String send = "";
		if (isBotAdmin(hostname) == true) {
			// get MXBean stuff
			RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
			if(mx.getUptime() >= 60000) {
				String upTime = mx.getUptime() / 60000 + "";
				long upTime2 = (mx.getUptime() % 60000) / 1000;
				send = "I have been running for: " + upTime + " minutes and "+upTime2+" seconds.";
			} else {
				String upTime = mx.getUptime() / 1000 + "";
				send = "I have been running for: " + upTime + " seconds.";
			}
		} else {
			send = error[1];
		}
		return send;
	}

}
